/*
 * To change this license header, choose License Headers in Project Properties. To change this
 * template file, choose Tools | Templates and open the template in the editor.
 */
package chalmers.game.subterranean.ui.view;

import chalmers.game.subterranean.event.Bus;
import chalmers.game.subterranean.event.events.RestartGameEvent;
import chalmers.game.subterranean.event.events.player.PlayerLookEvent;
import chalmers.game.subterranean.event.events.player.PlayerMoveEvent;
import chalmers.game.subterranean.event.events.player.SetTorchSizeEvent;
import chalmers.game.subterranean.util.options.PlayerValues;
import chalmers.game.subterranean.util.options.Sprites;
import chalmers.game.subterranean.util.options.Variables;
import chalmers.game.subterranean.util.options.WorldValues;
import com.google.common.eventbus.Subscribe;
import javafx.animation.TranslateTransition;
import javafx.beans.property.DoubleProperty;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;

/**
 * 
 * @author TH
 */
public class Player {

  public final ImageView img;
  private final Circle torch;
  private final TranslateTransition mover;
  private boolean dead = false;

  public Player() {
    img = new ImageView(Sprites.PLAYER_SPRITE);
    img.yProperty().setValue(WorldValues.LEVEL_SIZE / 2 - WorldValues.BLOCK_SIZE / 2);

    mover = new TranslateTransition();
    mover.setDuration(Variables.ANIMATION_DURATION);
    mover.setNode(img);

    torch = Torch.getTorch(img.translateXProperty(), img.translateYProperty());
    torch.centerYProperty().setValue(WorldValues.LEVEL_SIZE / 2);
    torch.centerXProperty().setValue(WorldValues.BLOCK_SIZE / 2);

    Bus.INSTANCE.register(this);
  }

  public Circle getTorch() {
    return Torch.getTorch(null, null);
  }

  @Subscribe
  public void move(PlayerMoveEvent event) {
    mover.setToX(event.point.x * WorldValues.VIEW_TO_MODEL_SCALE);
    mover.playFromStart();
  }

  @Subscribe
  public void handlePlayerLook(PlayerLookEvent event) {
    double sc = img.scaleXProperty().get();
    switch (event.direction) {
      case NORTH:
        break;
      case SOUTH:
        break;
      case WEST:
        if (sc < 0) {
          img.setScaleX(-sc);
        }
        break;
      case EAST:
        if (sc > 0) {
          img.setScaleX(-sc);
        }
        break;
    }
  }

  @Subscribe
  public void handleSetTorchSize(SetTorchSizeEvent event) {
    Animate.anim(torch.radiusProperty(), event.radius, PlayerValues.TORCH_UPDATE_INTERVAL * 1000);
  }

  @Subscribe
  public void handleRestartGame(RestartGameEvent evt) {
    Bus.INSTANCE.unRegister(this);
  }

  /**
   * 
   * @author TH
   */
  protected static abstract class Torch {

    private static Circle torch;

    protected static Circle getTorch(DoubleProperty xProperty, DoubleProperty yProperty) {
      if (torch == null) {
        torch = new Circle(WorldValues.GAME_RESOLUTION_WIDTH * 1.2);
        torch.translateXProperty().bind(xProperty);
        torch.translateYProperty().bind(yProperty);
      }
      return torch;
    }
  }
}
